import socket


def get_url(url, port):
    # 建立 socket 连接
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 连接服务端
    client.connect((url, port))
    # 发送请求
    client.send("GET / HTTP/1.1\r\nHost:{}\r\nConnection:close\r\n\r\n".format(url).encode("utf-8"))
    # 返回数据
    rec = client.recv(4096)
    # headers
    headers, _ = rec.split(b'\r\n\r\n')
    # 打印headers
    print(headers.decode())
    # 关闭连接
    client.close()
    pass


if __name__ == '__main__':
    get_url('www.baidu.com', 80)
